import packageObj from "../../package.json";

/**
 * 返回日期
 * @returns {string}
 */
export function getDate(time) {
  var myDate = new Date();
  if (time) {
    myDate = new Date(time);
  }
  // 获取当前年
  var year = myDate.getFullYear();
  // 获取当前月
  var month = myDate.getMonth() + 1;
  // 获取当前日
  var date = myDate.getDate();
  var h = myDate.getHours(); // 获取当前小时数(0-23)
  var m = myDate.getMinutes(); // 获取当前分钟数(0-59)
  var s = myDate.getSeconds();
  // 获取当前时间
  return (
    year +
    "-" +
    conver(month) +
    "-" +
    conver(date) +
    " " +
    conver(h) +
    ":" +
    conver(m) +
    ":" +
    conver(s)
  );
}

function conver(s) {
  return s < 10 ? "0" + s : s;
}

/**
 * 判断对象是否为空
 * @param obj
 * @returns {boolean}
 */
export function isEmptyObject(obj) {
  for (var key in obj) {
    return false; // 返回false，不为空对象
  }
  return true; // 返回true，为空对象
}

/**
 * 睡觉函数
 * @param func
 * @param delay
 * @returns {function(...[*]=)}
 */
export function debounce(func, delay) {
  let timer;
  return function (...args) {
    if (timer) {
      clearTimeout(timer);
    }
    timer = setTimeout(() => {
      func.apply(this, args);
    }, delay);
  };
}

/**
 * 生成唯一码UUID
 * @returns {string}
 */
export function uuid() {
  var s = [];
  var hexDigits = "0123456789abcdef";
  for (var i = 0; i < 36; i++) {
    s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
  }
  s[14] = "4";
  s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1);
  s[8] = s[13] = s[18] = s[23] = "-";
  var uuid = s.join("");
  return "uuid" + uuid;
}

/**
 * 递归查找
 * @param key
 * @param value
 * @param list
 * @returns {*}
 */
export function getSpecalNameFromTree(key, value, list) {
  findDataFromTreeLists = [];
  findDataFromTree(key, value, list);
  return findDataFromTreeLists && findDataFromTreeLists[0];
}

// 递归用到的变量
let findDataFromTreeLists = [];

// 递归的方法
function findDataFromTree(key, value, list) {
  list.filter((item) => {
    if (item[key] === value) {
      findDataFromTreeLists.push(item);
    } else {
      if (item.children && item.children.length) {
        return findDataFromTree(key, value, item.children);
      }
    }
  });
}

const $utils = {
  localStorage: {
    setItem: (key, value) => {
      localStorage.setItem(packageObj.name + "-" + key, JSON.stringify(value));
    },
    getItem: (key, name) => {
      let value = localStorage.getItem(packageObj.name + "-" + key);
      if (name) {
        value = localStorage.getItem(name + "-" + key);
      }
      if (value) {
        return JSON.parse(value);
      } else {
        return value;
      }
    },
  },
  sessionStorage: {
    setItem: (key, value) => {
      sessionStorage.setItem(
        packageObj.name + "-" + key,
        JSON.stringify(value)
      );
    },
    getItem: (key, name) => {
      let value = sessionStorage.getItem(packageObj.name + "-" + key);
      if (name) {
        value = sessionStorage.getItem(name + "-" + key);
      }
      if (value) {
        return JSON.parse(value);
      } else {
        return value;
      }
    },
  },
};
export default $utils;
